using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using ToDoListWeb.Service;
using ToDoListWeb.Models;
using Microsoft.AspNetCore.Http;

namespace ToDoListWeb.Controllers
{
    public class MatterController : Controller
    {
        UserService _userService = new UserService();
        InventoryService _inventoryService = new InventoryService();
        MatterService _matterService = new MatterService();

        /// <summary>
        /// 选择事项，并显示事项内容
        /// </summary>
        /// <param name="matterId"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult ChooseMatter(int matterId)
        {
            var matter = _matterService.GetMatter(matterId);
            return View(matter);
        }
        /// <summary>
        /// 显示事项备注
        /// </summary>
        /// <param name="matterId"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult ShowMatterRemark(int matterId)
        {
            var matter = _matterService.GetMatter(matterId);
            return View(matter);
        }
        /// <summary>
        /// 更改事项完成状态
        /// </summary>
        /// <param name="matterId"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult ChangeIsFinished(int matterId)
        {
            _matterService.ChangeIsFinished(matterId);
            var matters = _matterService.GetMatters(_matterService.GetMatter(matterId).InventoryId);
            return View(matters);
        }
        [HttpGet]
        public IActionResult ChangeIsFinished(int inventoryId, int matterId)
        {
            _matterService.ChangeIsFinished(matterId);
            var matters = _matterService.GetMatters(_matterService.GetMatter(matterId).InventoryId);
            return RedirectToAction("ShowAllMatters");
        }
        /// <summary>
        /// 获取事项详细信息
        /// </summary>
        /// <param name="matterId"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult GetDetailMatter(int matterId)
        {
            var matterModel = _matterService.GetDetailMatterModel(_matterService.GetMatter(matterId));
            return View(matterModel);
        }
        /// <summary>
        /// 添加事项（视图）
        /// 提供表格添加事项
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult AddMatter()
        {
            _userService.UpdateUserModel(GetUser());
            return View(GetUser().Inventory.ToList());
        }
        /// <summary>
        /// 创建事项（方法）
        /// </summary>
        /// <param name="inventoryId"></param>
        /// <param name="matterContent"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult FoundMatter(int inventoryId, string matterContent)
        {
            if (inventoryId == 0 || matterContent == null)
                return View("ChangeIsFinished", null);
            _matterService.CreateMatter(inventoryId, matterContent);
            var matters = _inventoryService.GetInventoryById(inventoryId).Matter.ToList();
            return View("ChangeIsFinished", matters);
        }
        /// <summary>
        /// 删除事项
        /// </summary>
        /// <param name="matterId"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult DeleteMatter(int matterId)                                                 //删除事件----方法
        {
            var inventoryId = _matterService.GetMatter(matterId).InventoryId;
            _matterService.DeleteMatter(matterId);
            var matters = _inventoryService.GetInventoryById(inventoryId).Matter.ToList();
            return View("ChangeIsFinished", matters);
        }
        [HttpGet]
        public IActionResult DeleteMatter(int inventoryId, int matterId)                                                 //删除事件----方法
        {
            _matterService.DeleteMatter(matterId);
            var matters = _inventoryService.GetInventoryById(inventoryId).Matter.ToList();
            return RedirectToAction("ShowAllMatters");
        }
        /// <summary>
        /// 创建CompileMatterModel（内部方法）
        /// 用于创建编辑事项的Model
        /// </summary>
        /// <param name="matterId"></param>
        /// <returns></returns>
        private CompileMatterModel GetCompileMatterModel(int matterId)                                  //构造CompileMatterModel----内部方法
        {
            CompileMatterModel cmm = new CompileMatterModel
            {
                User = GetUser(),
                Matter = GetUser().Matter.SingleOrDefault(m => m.MatterId == matterId)
            };
            return cmm;
        }
        /// <summary>
        /// 编辑事项（方法）
        /// </summary>
        /// <param name="inventoryId"></param>
        /// <param name="matterId"></param>
        /// <param name="matterContent"></param>
        /// <param name="matterRemark"></param>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public IActionResult CompileMatter(int inventoryId, int matterId, string matterContent, string matterRemark, string dateTime)//编辑事件----方法
        {
            _matterService.CompileMatterModel(inventoryId, matterId, matterContent, matterRemark, dateTime);
            return View("ShowEvents", GetUser());
        }
        /// <summary>
        /// 编辑事项（视图）
        /// </summary>
        /// <param name="matterId"></param>
        /// <returns></returns>
        public IActionResult CompileMatterView(int matterId)                                            //编辑事件----视图
        {
            if (matterId == 0)
                return View("ShowEvents", GetUser());
            var cmm = GetCompileMatterModel(matterId);
            return View(cmm);
        }
        /// <summary>
        /// 查看事项
        /// </summary>
        /// <param name="inventoryId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ShowMatters(int inventoryId)
        {
            var inventory = _inventoryService.GetInventoryById(inventoryId);
            return View(inventory);
        }
        /// <summary>
        /// 添加事项
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="typeId"></param>
        /// <param name="inventoryId"></param>
        /// <param name="matterName"></param>
        /// <param name="overTime"></param>
        /// <param name="reamark"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult CreateMatter(Inventory inventory, string matterContent, string remark, DateTime overTime)
        {
            if (matterContent == null || overTime == null || remark == null)
            {
                ViewData["Message"] = "输入不能为空!";
                return View("Matters", inventory);
            }
            else
            {
                _matterService.AddMatter(inventory.InventoryId, matterContent, overTime);
                _userService.UpdateUserModel(GetUser());
                return View("ShowEvents", GetUser());
            }
        }
        /// <summary>
        /// 显示所有事项
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ShowAllMatters()
        {
            var matterViewModels = _matterService.ConvertEntityModelToViewModel(GetUser().UserId);
            return View(matterViewModels);
        }
        /// <summary>
        /// 展示过期事项
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ShowPastdueMatters()
        {
            var matterViewModels = _matterService.ConvertEntityModelToViewModel(GetUser().UserId);
            return View(matterViewModels);
        }
        /// <summary>
        /// 显示相同创建时间的事项
        /// </summary>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <param name="day"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult ShowSameDateMatters(int year, int month, int day)
        {
            var matters = _matterService.ConvertEntityModelToViewModel(GetUser().UserId);
            var mattersViewModels = matters.Where(matter => matter.CreatTime.Year == year &&
            matter.CreatTime.Month == month && matter.CreatTime.Day == day).ToList();
            return View("ShowAllMatters", mattersViewModels);
        }
        /// <summary>
        /// 搜索页
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult SearchPage()
        {
            return View();
        }
        /// <summary>
        /// 搜索事项
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public IActionResult Search(string text)
        {
            if (text == null)
                text = " ";
            var matters = _matterService.GetAllOfUserMatters(GetUser().UserId);
            var mattersInclude = _matterService.SearchMatters(text, matters);
            return View(mattersInclude);
        }
        /// <summary>
        /// 还有3天过期的事项
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult UpcomingOverTime()
        {
            var matters = _matterService.GetAllOfUserMatters(GetUser().UserId);
            return View(matters);
        }

        UserModel GetUser()
        {
            int userId = (int)HttpContext.Session.GetInt32("userId");
            return _userService.GetUserModel(userId);
        }
    }
}